package Tools;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.sql.*;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource ds;
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\main\\java\\Druid.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            ds= DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
        if (resultSet !=null){
            resultSet.close();
        }
        if (statement !=null){
            statement.close();
        }
        if (connection !=null){
            connection.close();
        }

    }

}
